package org.jeecg.modules.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.AliyunOssHelper;
import org.jeecg.common.util.StringUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.entity.SysLog;
import org.jeecg.modules.system.entity.SysRole;
import org.jeecg.modules.system.service.ISysLogService;
import org.jeecg.modules.system.vo.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 文件上传
 * </p>
 *
 * @Author liurui
 * @since 2019-11-25
 */
@RestController
@RequestMapping("/b/sys/upload")
@Slf4j
public class SysUploadController {


	/**
	 * 共通图片上传 访问url: /basedata/sys/upload/image
	 * @param files
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("image")
	public Result<?> image(MultipartFile[] files) throws IOException {
		String imageStr = "";
		for (MultipartFile s : files){
			// 上传图片的原始名称
			String originalFilename = s.getOriginalFilename();
			String key = "pic/";
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			String ymd = sdf.format(new Date());
			String ym = ymd.substring(0,6);
			ymd = ymd.substring(6);
			// 新的图片名称(真实名称)
			String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
			// 新图片的路径
			key += ym + "/" + ymd + "/" + newFileName;
			boolean pic_flg = AliyunOssHelper.put(key, s.getInputStream());
			if (pic_flg) {
				if (StringUtils.isBlank(imageStr)){
					imageStr = AliyunOssHelper.getUrlPath(key);
				} else {
					imageStr += ";" + AliyunOssHelper.getUrlPath(key);
				}
			} else {
				return Result.error("上传异常");
			}
		}

		Result result = new Result();
		result.setMessage(imageStr);
		result.setResult(imageStr);
		return result;
	}



	/**
	 * 共通文件上传 访问url: /basedata/sys/upload/file
	 * @param files
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("file")
	public Result<?> file(MultipartFile[] files) throws IOException {
		String imageStr = "";
		for (MultipartFile s : files){
			// 上传图片的原始名称
			String originalFilename = s.getOriginalFilename();
			String key = "file/";
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			String ymd = sdf.format(new Date());
			String ym = ymd.substring(0,6);
			ymd = ymd.substring(6);
			// 新的图片名称(真实名称)
			String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
			// 新图片的路径
			key += ym + "/" + ymd + "/" + newFileName;
			boolean pic_flg = AliyunOssHelper.put(key, s.getInputStream());
			if (pic_flg) {
				if (StringUtils.isBlank(imageStr)){
					imageStr = AliyunOssHelper.getUrlPath(key);
				} else {
					imageStr += ";" + AliyunOssHelper.getUrlPath(key);
				}
			} else {
				return Result.error("上传异常");
			}
		}
		Result result = new Result();
		result.setMessage(imageStr);
		result.setResult(imageStr);
		return result;
	}



	/**
	 * app上传专用
	 * @param files
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("appFile")
	public Result<?> appFile(MultipartFile[] files) throws IOException {
		String imageStr = "";
		for (MultipartFile s : files){
			// 上传图片的原始名称
			String originalFilename = s.getOriginalFilename();
			String key = "appFile/";
			// 新的图片名称(真实名称)
			String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
			// 新图片的路径
			key += newFileName;
			boolean pic_flg = AliyunOssHelper.put(key, s.getInputStream());
			if (pic_flg) {
				if (StringUtils.isBlank(imageStr)){
					imageStr = AliyunOssHelper.getUrlPath(key);
				} else {
					imageStr += ";" + AliyunOssHelper.getUrlPath(key);
				}
			} else {
				return Result.error("上传异常");
			}
		}
		Result result = new Result();
		result.setMessage(imageStr);
		result.setResult(imageStr);
		return result;
	}
}
